package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.DesignerCategoryAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.MessageBean;
import com.dinadi.yidi.bean.TotalDesignerRequest;
import com.dinadi.yidi.bean.TotalDesignerRequestEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/6/21   设计师分类
 * Time: 14:02
 */
public class DesignerCategoryActivity extends BaseActivity {
    private View view;
    private TextView tvTitle;
    private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
    private DesignerCategoryAdapter mRefreshAdapter;
    private List<TotalDesignerRequestEntity> dataList = new ArrayList<>();
    private int page = 1;
    private String userId, token;
    private String category;


    @Override
    protected View getLayout() {
        view = LayoutInflater.from(DesignerCategoryActivity.this).inflate(R.layout.activity_message, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {
        initPullrefresh();
    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView);
        mPullLoadMoreRecyclerView.setGridLayout(1);
        mRefreshAdapter = new DesignerCategoryAdapter(this);
        mPullLoadMoreRecyclerView.setAdapter(mRefreshAdapter);
        mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreListener());
    }

    @Override
    protected void initListener() {
        mRefreshAdapter.setmOnItemClickListener(new DesignerCategoryAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, String data) {
                Intent intent = new Intent();
                intent.setClass(DesignerCategoryActivity.this, DesignerHmePageActivity.class);
                intent.putExtra("dUserId", data);
                intent.putExtra("type", "2");
                startActivity(intent);
            }
        });

    }


    @Override
    protected void initData() {
        userId = SharePreferencesHelper.getInstance(DesignerCategoryActivity.this).getString("userId");
        token = SharePreferencesHelper.getInstance(DesignerCategoryActivity.this).getString("token");
        Intent intent = getIntent();
        category = intent.getStringExtra("category");
        if (category.equals("9")) {
                   /*中国样*/
            tvTitle.setText("中国养");
        } else if (category.equals("1")) {
                    /*推荐*/
            tvTitle.setText("推荐");
        } else if (category.equals("2")) {
                   /*最新*/
            tvTitle.setText("最新");
        } else if (category.equals("3")) {
                   /*家具*/
            tvTitle.setText("家具");
        } else if (category.equals("4")) {
                   /*居家*/
            tvTitle.setText("居家");
        } else if (category.equals("5")) {
                   /*配饰*/
            tvTitle.setText("配饰");
        } else if (category.equals("6")) {
                   /*童趣*/
            tvTitle.setText("童趣");
        } else if (category.equals("7")) {
                   /*诗酒茶香*/
            tvTitle.setText("诗酒茶香");
        } else if (category.equals("8")) {
                   /*文博艺术*/
            tvTitle.setText("文博艺术");
        }

            initPullrefresh();

    }

    @Override
    protected void clickEvent(View v) {

    }

    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            closeLoadingDialog();
            changePager(2);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                TotalDesignerRequest bean = JsonUtil.parseJsonToBean(response, TotalDesignerRequest.class);
                dataList = bean.getDesignerList();
                mRefreshAdapter.addAllData(dataList);
                getData();
                String[] str;
                str = new String[dataList.size()];
                for (int i = 0; i < str.length; i++) {
                    String name = dataList.get(i).getNickName();
                    //   str = new String[i];
                    str[i] = name;
                }
                SharePreferencesHelper.getInstance(DesignerCategoryActivity.this).setSharedPreference("data", str);

            } else {
                toast(errMsg);
            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            changePager(1);
            closeLoadingDialog();
            toast("加载失败");
        }
    };

    private void getData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                //刷新结束
                mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            }
        }, 1000);
    }


    class PullLoadMoreListener implements PullLoadMoreRecyclerView.PullLoadMoreListener {
        @Override
        public void onRefresh() {
            setRefresh();
        }

        @Override
        public void onLoadMore() {
            page = page + 1;
            initPullrefresh();
        }
    }

    private void setRefresh() {
        mRefreshAdapter.clearData();
        page = 1;
        initPullrefresh();
    }

    private void initPullrefresh() {
        showLoadingDialog("");
        MessageBean bean = new MessageBean();
        bean.setToken(token);
        bean.setUserId(userId);
        bean.setPageNum(page + "");
        bean.setPageSize("9");
        bean.setType("2");
        bean.setCategory(category);
        CommonInterface messageInterface = new CommonInterface(DesignerCategoryActivity.this, httpListener);
        messageInterface.getInterface("13001", JSON.toJSONString(bean));
    }
}
